
(function() {

	/*------------------------- 页面内容配置 -------------------------*/
	/**
	 * Example:    {
	 * 		video:    'video url',
	 * 		videoImg: 'videoImg url',
	 * 		title:    'title',
	 *		subTitle: '911carrera4s, 911turbo',
	 * 		content:  'content',
	 * 		buttons:  'id0, id1'
	 * }
	 */

	var list    = {
		/* sport*/
		"911": {
			video:    './video/sport/911.mp4',
			title:    '911',
			subTitle: '911_title2',
			content:  '911_desc'
		},
		boxster: {
			video:    './video/sport/boxster.mp4',
			title:    'boxster',
			subTitle: 'boxster_title2',
			content:  'boxster_desc'
		},
		cayman: {
			video:    './video/sport/cayman.mp4',
			title:    'cayman',
			subTitle: 'cayman_title2',
			content:  'cayman_desc'
		},
		/* family*/
		macan: {
			video:    './video/family/macan.mp4',
			title:    'macan',
			subTitle: 'macan_title2',
			content:  'macan_desc'
		},
		cayenne: {
			video:    './video/family/cayenne.mp4',
			title:    'cayenne',
			subTitle: 'cayenne_title2',
			content:  'cayenne_desc'
		},
		panamera: {
			video:    './video/family/panamera.mp4',
			title:    'panamera',
			subTitle: 'panamera_title2',
			content:  'panamera_desc'
		},
			/* lifestyle*/
		travel: {
			video:    './video/lifestyle/travel.mp4',
			title:    'travel',
			// subTitle: 'travel_title2',
			content:  'travel_desc'
		},
		pds: {
			video:    './video/lifestyle/pds.mp4',
			title:    'pds',
			// subTitle: 'pds_title2',
			content:  'pds_desc'
		},
		golf: {
			video:    './video/lifestyle/golf.mp4',
			title:    'golf',
			// subTitle: 'golf_title2',
			content:  'golf_desc'
		},
		/* principle*/
		principle: {
			video:    './video/movie/DNA.mp4',
			title:    'Principle_Drive',
			// subTitle: 'Principle_title2',
			content:  'Principle_desc'
		},
		/* snowforce*/
		snowforce: {
			video:    './video/exp/snowforce.mp4',
			title:    'snowforce_Drive',
			subTitle: 'snowforce_title2',
			content:  'snowforce_desc'
		},
		/* driving_school*/
		driving_school: {
			video:    './video/exp/driving_school.mp4',
			title:    'driving_school_in_line',
			subTitle: 'driving_school_title2',
			content:  'driving_school_desc'
		},
		/* lemans*/
		lemans: {
			video:    './video/motorsport/lemans/lemans.mp4',
			title:    '24hlemans',
			// subTitle: 'lemans_title2',
			content:  'lemans_desc'
		},
		/*pcca*/
		pcca: {
			video:    './video/motorsport/pcca/pcca.mp4',
			title:    'pcca_Drive',
			// subTitle: 'pcca_title2',
			content:  'pcca_desc'
		},
		//dealer
		dealer: {
			videoImg: './image/pcpuxi_kv.jpg',
			title:    'dealer_title',
			subTitle: '',
			content:  'dealer_description'
		},
		Financial_services: {
			video:    './video/service/Financial_services/Financial_services.mp4',
			title:    'finance',
			// subTitle: 'Financial_services_title2',
			content:  'Financial_services_desc'
		},
		
		defaultTo: {
			videoImg: './image/principle/principle.jpg',
			video:    './video/exp/snowforce.mp4',
			title:    'detail-title-%s',
			subTitle: '',
			content:  'detail-content-%s',
			buttons:  ''
		}
	};
	/*------------------------- 页面内容配置 -------------------------*/

	list.getBtns = function(id, classID) {
		id       = id + '';
		var btns = [];

		if(_.contains(['911','boxster','cayman','macan','cayenne','panamera'], id)) {
			btns = [
				{name: 'test_drive_r', txt: 'overlay_Drive'},
				{name: 'car_'+id+'_icon', txt:  util.wordCap(id)+' '+(i18n.getLang()=='en'?' Models':' 车型')},
				{name: 'features_icon', txt: 'features'}
			];
		}else if(id == 'dealer') {
			btns = [
				{id: 'contact-puxicenter', name: 'contact_btn', txt: 'contacts'},
				{name: 'stock_btn', txt:  'stocklist'},
				{name: 'cinema_btn', txt: 'movie_line'},
				{name: 'floorplan_btn', txt: 'floorplan_s'}
			];
		}else if(id == 'travel') {
			btns = [
				{id:'contact-travel', name: 'contact_btn', txt: 'contacts'},
			];
		}else if(id == 'golf') {
			btns = [
				{id:'contact-golf', name: 'contact_btn', txt: 'contacts'},
			];
		}else if(id == 'pds') {
			btns = [
				// {name: 's_a_pds_icon', txt: 'goods_button'}
			];
		}else if(id == 'Financial_services') {
			btns = [
				{id: 'contact-financial', name: 'contact_btn', txt: 'contacts'}
			];
		}else if(id == 'snowforce') {
			btns = [
				{id: 'contact-snowforce', name: 'contact_btn', txt: 'contacts'}
			];
		}else if(id == 'driving_school') {
			btns = [
				{id: 'contact-driving', name: 'contact_btn', txt: 'contacts'}
			];
		}

		var specialBtn = {
			sport:     [{name: 'main_motorsport_icon', txt: 'motorsport_line'}],
			family:    [{name: 'main_s_a_icon', txt: 'service_accessories_line'}],
			lifestyle: [{name: 'main_events_icon', txt: 'porsche_events_line'}],
			events:    [{name: 'main_events_icon', txt: 'porsche_events_line'}]
		};

		if(classID && specialBtn[classID]) {
			return btns.concat(specialBtn[classID] || []);
		}else
			return btns;
	};

	list.getData = function(id) {
		id += '';
		// 0.id没有值，则值为空
		if(!id) {
			return null;
		}

		var data = list[id] ? list[id] : list['defaultTo'];
		data     = $.extend(true, {id: id}, data);

		_.each(data, function(v, k) {
			data[k] = util.format(v, id);
			if(k == 'title' || k=='content') {
				data[k] = __(data[k]);
				if(k == 'title' && _.contains(['911','boxster','cayman','macan','cayenne','panamera'], id)) {
					data[k] = '<div class="img-title" style="background-image:url(./image/stock/text/stock_'+id+'_text_b.png)"></div>';
				}
			}
			if(k == 'video' && v) {
				data['video_preview'] = v.replace('.mp4', '_preview.mp4');
			}
			if(k == 'subTitle' && v) {
				var array = v.split(',');
				data[k]   = _.map(array, function(_v) {return __(_v.trim())});
			}
		});
		return data;
	};


	var Detail = function() {
		this.data = null;
	};
	Detail.prototype.onCreate = function() {
		this.$video        = this.$el.find('.videoImgWrap');
		this.$titleWrap    = this.$el.find('.titleWrap');
		this.$subTitleWrap = this.$el.find('.subTitleWrap');
		this.$contentWrap  = this.$el.find('.contentWrap');
		this.$buttonWrap   = this.$el.find('.buttonWrap');

		var that = this;
		this.$el.on('click', '.videoImgWrap', function() {
			if(that.data.video){
				if(that.data && that.data.video)
				window.clearTimeout(video_set);
				var video_set=window.setTimeout(function() {
					pf.overlay('play_video');
				},200);
				// kpi
				kpi.store('videos', that.data.id);
				
				pcc2.playVideo(that.data.video);
			}
		});
		$(document).on('click', '.video-zoon', function() {
			// detail video_play();
			// $("body").removeClass("white_border");
			// pcc2.css3(".closeVieoBtn .x_video");
			// window.setTimeout(function() {
			// 	pcc2.$video.fadeOut();
			// 	pcc2.$video.empty();
			// 	var myVideo = document.getElementsByTagName('video')[0];
			// 	if (myVideo) {
			// 		myVideo.play();
			// 	}
			// }, 500);
		});
	};

	Detail.prototype.onShow = function(params) {
		if (params.detailID=="lemans") {
			// pcc2.light('on');
			$('body').addClass("on");
		}else if(params.detailID=="pcca"){
			// pcc2.light('on');
			$('body').addClass("on");
		}else{
			if ($('body').hasClass('on')) {
				// pcc2.light('off');
				$('body').removeClass('on')
			}
		}
		$("#play_video").attr("video_name",params.detailID);
		// this.$el.show();
		$("#detail").attr("page_show",params.detailID);
		if(params && params.detailID) {
			var btns = list.getBtns(params.detailID, params.classID);
			this.render(list.getData(params.detailID), btns);
			// this.previewVideo();
		}
	};

	Detail.prototype.previewVideo = function() {
		// var url = that.data.video;
		
	};
	Detail.prototype.onHide = function() {
		// this.$el.hide();
		this.$video.empty();
		this.$titleWrap.empty();
		this.$subTitleWrap.empty().hide();
		this.$contentWrap.empty();
		this.$buttonWrap.empty();
	};
	Detail.prototype.render = function(data, btns) {
		this.data = data;
		if(data) {
			var videoHtml = '';
			if(data && data.video){
				var tipHtml = '';
				tipHtml = '<div class="video-tip"><div class="video-tip_img"></div>'+__('videoTip')+'</div>';

				videoHtml = '<div class="video_play sprite icon-btn"></div>'+tipHtml+'<video class="vide_id" style="  width: 100%;height: 100%;" loop="loop" autoplay="autoplay"><source src='+ data.video_preview +' type="video/mp4"></video>';
			}else if(data && data.videoImg){
				videoHtml = '<img src="'+data.videoImg+'" />';
			}
			var subTitleHtml = data.subTitle ? data.subTitle.join('<br/>') : '';
			
			this.$video.html(videoHtml);	
			this.$titleWrap.html(data.title);
			if(subTitleHtml)
				this.$subTitleWrap.show().html(subTitleHtml);
			this.$contentWrap.html(data.content);

			if(btns) {
				icons.render(this.$buttonWrap, btns);
			}
		}else {
			this.$video.empty();
			this.$titleWrap.empty();
			this.$subTitleWrap.empty().hide();
			this.$contentWrap.empty();
			this.$buttonWrap.empty();
		}
	};

	var detail  = pf.newPage('detail');
	var _detail = pf.newPage('-detail');
	util.extend(detail, new Detail());
	util.extend(_detail, new Detail());
})();